﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RailwayReservationWeb.manager
{
    public partial class user_add : System.Web.UI.Page
    {
        private RailwayReservationDataContext db;

        protected void Page_Load(object sender, EventArgs e)
        {
            db = new RailwayReservationDataContext();
            pMessage.Visible = false;
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (Page.IsValid)
                {
                    var ob = new Account();
                    var asv = new AuthService();
                    string userName = txtUserName.Text.Trim();
                    string passw = Checksum.MD5(txtPassword.Text);
                    if (asv.CheckUserName(userName) == true)
                    {
                        txtUserName.Focus();
                        throw new Exception("This Username already exists");
                    }
                    ob.UserName = userName;
                    ob.Password = txtPassword.Text.Trim();
                    ob.Email = txtEmail.Text.Trim();
                    ob.FullName = txtFullName.Text.Trim();
                    ob.SSN = txtSSN.Text.Trim();
                    ob.DoB = DateTime.Parse(txtDob.Text.Trim());
                    ob.Gender = Int16.Parse(cboGender.SelectedValue);
                    ob.Address = txtAddress.Text.Trim();
                    ob.Phone = txtPhone.Text.Trim();
                    ob.Role = Int16.Parse(cboRole.SelectedValue);
                    ob.Status = 1;
                    db.Accounts.InsertOnSubmit(ob);
                    db.SubmitChanges();
                    Response.Redirect("~/manager/user_list.aspx");
                }
            }
            catch (Exception ex)
            {
                ShowMessage(ex.Message);
            }
        }

        private void ShowMessage(string msg)
        {
            pMessage.Visible = true;
            pMessage.InnerText = msg;
        }
    }
}